package com.fun_ordering.controller;

import com.fun_ordering.components.ApiResponse;
import com.fun_ordering.components.PageResult;
import com.fun_ordering.pojo.DishType;
import com.fun_ordering.service.DishTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 菜品类型表controller
 * @author XiaoPi
 *
 */
@RestController
@RequestMapping("/dishType")
public class DishTypeController {

	@Autowired
	private DishTypeService dishTypeService;

	/**
	 * 返回全部列表
	 * @return
	 */
	@RequestMapping("/findAll")
	public ApiResponse<List<DishType>> findAll(){
		return ApiResponse.success(dishTypeService.findAll());
	}

	/**
	 * 返回分类的菜单列表
	 * @return
	 */
	@RequestMapping("/getMenu")
	public List<DishType> getMenu(){
//		System.out.println("调用/dishType/getMenu");
		return dishTypeService.selectDishListByType();
	}


	/**
	 * 返回全部列表
	 * @return
	 */
	@RequestMapping("/findPage")
	public PageResult  findPage(int page,int rows){

		return dishTypeService.findPage(page, rows);
	}

	/**
	 * 增加
	 * @param dishType
	 * @return
	 */
	@RequestMapping("/add")
	public ApiResponse add(@RequestBody DishType dishType){
		try {
			dishTypeService.add(dishType);
		return ApiResponse.success(null);
		} catch (Exception e) {
			e.printStackTrace();
		return ApiResponse.failed(null);
		}
	}

	/**
	 * 修改
	 * @param dishType
	 * @return
	 */
	@RequestMapping("/update")
	public ApiResponse update(@RequestBody DishType dishType){
		try {
			dishTypeService.update(dishType);
			return ApiResponse.success(null);
		} catch (Exception e) {
			e.printStackTrace();
			return ApiResponse.failed(null);
		}
	}

	/**
	 * 获取实体
	 * @param typeId
	 * @return
	 */
	@RequestMapping("/findOne")
	public DishType findOne(Integer typeId){
		return dishTypeService.findOne(typeId);
	}

	/**
	 * 批量删除
	 * @param typeIds
	 * @return
	 */
	@RequestMapping("/delete")
	public ApiResponse delete(Integer [] typeIds){
		try {
			dishTypeService.delete(typeIds);
			return ApiResponse.success(null);
		} catch (Exception e) {
			e.printStackTrace();
			return ApiResponse.failed(null);
		}
	}

		/**
	 * 查询+分页
	 * @param dishType
	 * @param page
	 * @param rows
	 * @return
	 */
	@RequestMapping("/pageSearch")
	public PageResult search(@RequestBody DishType dishType, int page, int rows  ){
//		System.out.println(dishType);
		return dishTypeService.findPage(dishType, page, rows);
	}

}
